package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: sky-take-out
 * @Package: com.sky.service.impl
 * @ClassName: SetmealServiceImpl
 * @Author: 王加其
 * @Date: 2023/4/19 10:55
 * @Version: 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void saveWithDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //拷贝数据
        BeanUtils.copyProperties(setmealDTO,setmeal);
        if(setmealDTO.getStatus() == null){
            setmeal.setStatus(StatusConstant.ENABLE);
        }
        //先往套菜表添加数据（注意id返回）
        this.setmealMapper.save(setmeal);
        //再往setmeal_dish中添加n条菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //前端的菜品是必填,获取返回的id后填进去
        setmealDishes.forEach(s -> s.setSetmealId(setmeal.getId()));
        this.setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = this.setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getByIdWithDishes(id);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //拷贝属性
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //先修改套餐
        this.setmealMapper.update(setmeal);
        //再删旧，关联的菜品
        List ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        this.setmealDishMapper.deleteBatch(ids);
        //重新插入新的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(s -> s.setSetmealId(setmeal.getId()));
        setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否起售，起售不能删
        Integer count = this.setmealMapper.countStatusByIds(ids);
        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //不用判断其他关联了
        //直接删除选中套餐
        this.setmealMapper.deleteBatch(ids);
        //再删除关联表中的数据，setmeal_dish
        this.setmealDishMapper.deleteBatch(ids);
    }

    /**
     * 停售，起售
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        //判断菜品是否停售售，含有停售的菜品的套餐不能起售
        if (setmeal.getStatus()==1){
            SetmealDTO setmealDTO = this.setmealMapper.CountStatusById(id);
            if(setmealDTO.getSetmealDishes().size() > 0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        this.setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    @Cacheable(cacheNames = "setmeal",key = "#setmeal.categoryId")
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
